﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.Security.Cryptography;
using System.Text.RegularExpressions;

namespace vkontakte_client
{
    public class People
    {
        public string First_name;
        public string Last_name;
        public string uid;
        public string photo;

        public static System.Collections.Generic.List<People> GetFriendFromXML(string xml)
        {
            System.Collections.Generic.List<People> dataList = new System.Collections.Generic.List<People>();

            string UserBlockSearchPattern = "<user>+?.+?</user>+?";
            Regex UserBlockSearchRegex = new Regex(UserBlockSearchPattern);
            MatchCollection Matches = UserBlockSearchRegex.Matches(xml.Replace("\n", "w"));
            for (int user = 0; user < Matches.Count; user++)
            {
                People CurrentPeople = new People();
                string CurrString = Matches[user].Value;
                ///определяем First_name
                Match FirstNameMatch = Regex.Match(CurrString, "<first_name>+?.+?</first_name>+?");
                CurrentPeople.First_name = (FirstNameMatch.Value.Replace("<first_name>", "")).Replace("</first_name>", "");
                ///определяем Last_name
                Match LastNameMatch = Regex.Match(CurrString, "<last_name>+?.+?</last_name>+?");
                CurrentPeople.Last_name = (LastNameMatch.Value.Replace("<last_name>", "")).Replace("</last_name>", "");
                ///определяем uid
                Match uidMatch = Regex.Match(CurrString, "<uid>+?.+?</uid>+?");
                CurrentPeople.uid = (uidMatch.Value.Replace("<uid>", "")).Replace("</uid>", "");
                ///определяем Photo    
                Match photoMatch = Regex.Match(CurrString, "<photo+?.+?</photo>+?");
                CurrentPeople.photo = (photoMatch.Value.Replace("<photo>", "")).Replace("</photo>", "");
                dataList.Add(CurrentPeople);
            }            
            return dataList;
        }
        public static System.Collections.Generic.List<People> GetFriends(string uid)
        {
            string xml = Http.StupidGet("friends.get", "uid=" + uid +"&fields=uid,photo&order=hints");
            return GetFriendFromXML(xml);
        }
        public static System.Collections.Generic.List<People> GetOnlineFriends(string uid)
        {
            string xml = Http.StupidGet("friends.getOnline", "uid=" + uid + "&fields=uid,photo&order=hints");
            return GetFriendFromXML(xml);
        }
    }
}
